#!/bin/bash
echo "*********************************************************************************"
echo "*                             1.backup now                                      *"
echo "*                             2.set time to backup                              *"
echo "*                             3.check backup file                               *"
echo "*                             4.clear the settings of backup                    *"
echo "*                             5.qiut                                            *"
echo "*********************************************************************************"
echo "please input a number (1-5):"
read choice
function1()
{
month=`date +%m`
day=`date +%d`
year=`date +%Y`
hour=`date +%H`
min=`date +%M`
filename=$year-$month-$day-$hour-$min
tmp1=1
while [ "$tmp1" = 1 ]
do
  echo "**********************************************************************************"
  echo "please input the path of the file that was backup:                               *"
  echo "**********************************************************************************"
  read path1
  if [ ! -d "$path1" ];then
    echo "ERROR: No such file or directory"
  else
    tmp1=2
  fi
done
headname=`basename $path1`

tmp2=1
while [ "$tmp2" = 1 ]
do
  echo "**********************************************************************************"
  echo "please input the path of the backup file:                                        *"
  echo "**********************************************************************************"
  read path2
  if [ ! -d "$path2" ];then
    echo "ERROR:No such file or directory"
  else
    tar zcvf $path2/$headname$filename.tgz $path1
    backup_time=date
    tmp2=2
  fi
done
if [ $? -eq 0 ];then
   echo "bakeup complete!"
   echo $filename "backup complete!" >>/root/backup_log
else
   echo "backup failure! please backup again!"
   echo $filename "backup failure!" >>/root/backup_log
fi
}

function2()
{
month=`date +%m`
day=`date +%d`
year=`date +%Y`
hour=`date +%H`
min=`date +%M`
filename=$year-$month-$day-$hour-$min
username=`id -un`
  echo "**********************************************************************************"
  echo "please input the time of backup:            (eg: 30 1 * * *)                     *"
  echo "**********************************************************************************"
  read backuptime
  echo "$backuptime"  $username /root/backup_in_time.sh >>/etc/crontab
tmp2=2
while [ "$tmp2" = 2 ]
do
  echo "**********************************************************************************"
  echo "please input the path of the file that was backup:                               *"
  echo "**********************************************************************************"
  read path1
  if [ ! -d "$path1" ];then
    echo "ERROR: No such file or directory!"
  else
    tmp2=3
  fi
done
headname=`basename $path1`

tmp3=3
while [ "$tmp3" = 3 ]
do
  echo "**********************************************************************************"
  echo "please input the path of the backup file:                                        *"
  echo "**********************************************************************************"
  read path2
  if [ ! -d "$path2" ];then
    echo "ERROR:No such file or directory"
  else
    echo '#!/bin/bash' >>/root/backup_in_time.sh
    echo 'month=`date +%m`' >>/root/backup_in_time.sh
    echo 'day=`date +%d`' >>/root/backup_in_time.sh
    echo 'year=`date +%Y`' >>/root/backup_in_time.sh
    echo 'hour=`date +%H`' >>/root/backup_in_time.sh
    echo 'min=`date +%M`' >>/root/backup_in_time.sh
    echo 'filename=$year-$month-$day-$hour-$min' >>/root/backup_in_time.sh
    echo 'tar zcvf' $path2/$headname'$filename.tgz' $path1 >>/root/backup_in_time.sh
    chmod +x /root/backup_in_time.sh
    tmp3=4
  fi
done
if [ $? -eq 0 ];then
   echo "set complete!"
   echo $filename "set time to backup complete!!!" >>/root/backup_log
else
   echo "set failure! please set again!"
   echo $filename "set time to backup failure!!!" >>/root/backup_log
fi
}

function3()
{
tmp1=1
while [ "$tmp1" = 1 ]
do
  echo "********************************************************************************"
  echo "the following is the backup file:                                              *"
  echo "********************************************************************************"
  find / -name '*.tgz'
  echo "********************************************************************************"
  echo "please input the filename you want check:                                      *"
  echo "********************************************************************************"
  read filename
  if [ ! -d "$filename" ];then
   tar tvf $filename
   tmp1=2
  else
   echo "ERORR:No such file!!!"
  fi
done
}

function4()
{
month=`date +%m`
day=`date +%d`
year=`date +%Y`
hour=`date +%H`
min=`date +%M`
filename=$year-$month-$day-$hour-$min
  echo >/root/backup_in_time.sh
  grep -v "backup_in_time.sh" /etc/crontab >/etc/crontab.sh
  cp /etc/crontab /etc/crontab.backup
  rm -rf /etc/crontab
  mv /etc/crontab.sh /etc/crontab
  if [ $? = 0 ];then
     echo "cancel complete!!!"
     echo $filename "cancel settings complete!!!" >>/root/backup_log
  else
     echo "cancel failure!!!, try again please!!!"
     echo $filename "cancel settings failure!!!" >>/root/backup_log
  fi
}

case $choice in
   1) function1;;
   2) function2;;
   3) function3;;
   4) function4;;
   5) exit 0;;
esac
****************************************************************************
